/*
 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "los_task.h"
#include "ohos_init.h"
#include "lz_hardware.h"

#define GPIO_SOILD     GPIO0_PB1
#define GPIO_WATE      GPIO0_PB0
#define GPIO_WILD      GPIO0_PA4
extern uint8_t iowild_status;
extern uint8_t iowater_status;
extern uint8_t iosoild_status;
static EVENT_CB_S g_gpio_event;

/***************************************************************
* 函数名称: gpio_process
* 说    明: gpio任务
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void gpio_init()
{
    LzGpioInit(GPIO_WILD);
    PinctrlSet(GPIO_WILD, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);  

    LzGpioInit(GPIO_WATE);
    PinctrlSet(GPIO_WATE, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);  
    
    LzGpioInit(GPIO_SOILD);
    PinctrlSet(GPIO_SOILD, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);  
    LOS_EventWrite(&g_gpio_event, 0x00000001);
}
void gpio_soild(){
    unsigned int ret = LZ_HARDWARE_SUCCESS;

    LOS_EventRead(&g_gpio_event, 0x00000001, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
    LzGpioValue soild = LZGPIO_LEVEL_LOW;
    ret=LzGpioSetDir(GPIO_SOILD, LZGPIO_DIR_IN);
    LzGpioSetVal(GPIO_SOILD, 0);
    if(ret != LZ_HARDWARE_SUCCESS){
        printf("gpio failed\n");
    }
    while (1)
    {
        soild = LZGPIO_LEVEL_LOW;
        ret=LzGpioGetVal(GPIO_SOILD, &soild);
        if(soild != LZGPIO_LEVEL_LOW){
            iosoild_status=0;
            printf("土壤不含水  %d\n",soild);
        }else{
            iosoild_status=1;
            printf("土壤含水    %d\n",soild);
        }
        LzGpioDir dir;
        ret =LzGpioGetDir(GPIO_SOILD, &dir);
        printf("GPIO_SOILD dir: %d\n", dir);
        sleep(5);
    }    
}
void gpio_wild()
{ 
    LOS_EventRead(&g_gpio_event, 0x00000001, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
    LzGpioValue value = LZGPIO_LEVEL_LOW;
    while (1)
    {
        if(iowild_status==0){
            printf("通风扇状态: 关\n");
            LzGpioSetDir(GPIO_WILD, LZGPIO_DIR_IN);
        }else{
            printf("通风扇状态: 开\n");
            LzGpioSetDir(GPIO_WILD, LZGPIO_DIR_OUT);
            LzGpioSetVal(GPIO_WILD, 1);
        }
        LzGpioGetVal(GPIO_WILD, &value);
        sleep(5);
        printf("\n");
    }
}
void gpio_water()
{ 
    LOS_EventRead(&g_gpio_event, 0x00000001, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
    LzGpioValue value = LZGPIO_LEVEL_LOW;
    while (1)
    {
        if(iowater_status==0){
            printf("水泵状态: 关\n");
            LzGpioSetDir(GPIO_WATE, LZGPIO_DIR_IN);
        }else{
            printf("水泵状态: 开\n");
            LzGpioSetDir(GPIO_WATE, LZGPIO_DIR_OUT);
            LzGpioSetVal(GPIO_WATE, 1);
        }
        LzGpioGetVal(GPIO_WATE, &value);
        sleep(5);
        printf("\n");
    }
}
/***************************************************************
* 函数名称: gpio_example
* 说    明: 开机自启动调用函数
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void gpio_example()
{
    unsigned int init_id;
    unsigned int wild_id;
    unsigned int water_id;
    unsigned int soild_id;
    TSK_INIT_PARAM_S init = {0};
    TSK_INIT_PARAM_S wild = {0};
    TSK_INIT_PARAM_S water = {0};
    TSK_INIT_PARAM_S soild = {0};
    unsigned int ret = LOS_OK;
    ret = LOS_EventInit(&g_gpio_event);
    if (ret != LOS_OK)
    {
        printf("Falied to create EventFlags\n");
        return;
    }
    /*gpio初始化 */
    init.pfnTaskEntry = (TSK_ENTRY_FUNC)gpio_init;
    init.uwStackSize = 2048;
    init.pcName = "gpio init process";
    init.usTaskPrio = 2;
    ret = LOS_TaskCreate(&init_id, &init);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
    /**风扇控制任务 */
    wild.pfnTaskEntry = (TSK_ENTRY_FUNC)gpio_wild;
    wild.uwStackSize = 2048;
    wild.pcName = "gpio wild process";
    wild.usTaskPrio = 3;
    ret = LOS_TaskCreate(&wild_id, &wild);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
    water.pfnTaskEntry = (TSK_ENTRY_FUNC)gpio_water;
    water.uwStackSize = 2048;
    water.pcName = "gpio water process";
    water.usTaskPrio = 4;
    ret = LOS_TaskCreate(&water_id, &water);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
    soild.pfnTaskEntry = (TSK_ENTRY_FUNC)gpio_soild;
    soild.uwStackSize = 2048;
    soild.pcName = "gpio soild process";
    soild.usTaskPrio = 5;
    ret = LOS_TaskCreate(&soild_id, &soild);
    if (ret != LOS_OK)
    {
        printf("Falied to create task ret:0x%x\n", ret);
        return;
    }
}

APP_FEATURE_INIT(gpio_example);